package org.springblade.oa.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.oa.entity.*;
import org.springblade.oa.service.*;
import org.springblade.oa.vo.*;
import org.springblade.oa.wrappper.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author LK
 * @version 1.0.0
 * @Title: ExpenseAccounController.java
 * @Description: TODO    报销控制
 * @date 2020/1/13 11:54
 */
@RestController
@AllArgsConstructor
@RequestMapping("/ea")
@Api(value = "报销", tags = "报销")
public class ExpenseAccountController {

	private IExpenseAccountService expenseAccountService;

	private IBxService bxService;
	private IBxfymxService bxfymxService;
	private IBxxcmxService bxxcmxService;
	private IBxzszpmxService bxzszpmxService;
	private IBxshService bxshService;

	/**
	 * 报销列表
	 *
	 * @param expenseAccount
	 * @param user
	 * @param pageQuery
	 * @return
	 */
	@GetMapping("exList")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "列表", notes = "传入expenseAccount、pageQuery")
	public R<IPage<ExpenseAccount>> getExpenseAccountList(ExpenseAccount expenseAccount, BladeUser user, PageQuery pageQuery) {
		IPage<ExpenseAccount> pages = expenseAccountService.getExpenseAccountList(expenseAccount, user, pageQuery);
		return R.data(pages);
	}

	/**
	 * 获取部门和项目名称列表联动
	 *
	 * @param eaCompany
	 * @return
	 */
	@GetMapping("projectName")
	public R<Map> getLinkageForDeptAndProjectName(Integer eaCompany) {
		return R.data(expenseAccountService.getProjectNameList(eaCompany));
	}

	/**
	 * 获取报销详情
	 *
	 * @param bx 报销对象
	 * @return
	 */
	@GetMapping("eaDetails")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "详情", notes = "传入bx")
	public R<Map> getExpenseAccountDetails(Bx bx) {
		Integer eaId = bx.getId();
		Bx detail = bxService.getOne(Condition.getQueryWrapper(bx));
		BxVo bxVo = null;
		Bxxcmx bxxcmx;
		Bxfymx bxfymx;
		Bxzszpmx bxzszpmx;
		List<Bxxcmx> bxxcmxList;
		List<Bxfymx> bxfymxList;
		List<Bxzszpmx> bxzszpmxList;
		List<Bxsh> bxshList;
		List<BxxcmxVO> bxxcmxVOList = null;
		List<BxfymxVO> bxfymxVOList = null;
		List<BxzszpmxVO> bxzszpmxVOList = null;
		List<BxshVO> bxshVOList = null;
		Map<String, Object> map = new HashMap();
		Set glgx =new HashSet();
		Integer glID ;
		String glzy ="";
		Integer gllx ;
		if (Func.notNull(detail)) {
			Integer eaType = detail.getBxlx();
			bxVo = BxWrapper.build().entityVO(detail);
			if (eaType == 1) {
				bxxcmx = new Bxxcmx();
				bxxcmx.setBxdid(eaId);
				bxxcmx.setIsDeleted(0);
				bxxcmxList = bxxcmxService.list(Condition.getQueryWrapper(bxxcmx));
				bxxcmxVOList = BxxcmxWrapper.build().listVO(bxxcmxList);
				//查询是否关联住宿专票
				bxzszpmx = new Bxzszpmx();
				bxzszpmx.setClbxdid(eaId);
				bxzszpmx.setIsDeleted(0);
				bxzszpmxList = bxzszpmxService.list(Condition.getQueryWrapper(bxzszpmx));
				if (bxzszpmxList!=null && bxzszpmxList.size()>0){
					for (int i = 0; i <bxzszpmxList.size() ; i++) {
						Map<String, Object> glmap = new HashMap();
//	如果是关联住宿 则返回关联的住宿报销单id 以及住宿报销单摘要
						glID = bxzszpmxList.get(i).getBxdid();
						glzy = bxService.getById(bxzszpmxList.get(i).getBxdid()).getBxabstract();
						gllx = 3;
						glmap.put("glID",glID);
						glmap.put("glzy",glzy);
						glmap.put("gllx",gllx);
						glgx.add(glmap);
					}
				}
			} else if (eaType == 2) {
				bxfymx = new Bxfymx();
				bxfymx.setBxdid(eaId);
				bxfymxList = bxfymxService.list(Condition.getQueryWrapper(bxfymx));
				bxfymxVOList = BxfymxWrapper.build().listVO(bxfymxList);
			} else if (eaType == 3) {
				bxzszpmx = new Bxzszpmx();
				bxzszpmx.setBxdid(eaId);
				bxzszpmxList = bxzszpmxService.list(Condition.getQueryWrapper(bxzszpmx));
				bxzszpmxVOList = BxzszpmxWrapper.build().listVO(bxzszpmxList);
				if (bxzszpmxList.get(0).getSfglxc() == 1&&bxzszpmxList.get(0).getClbxdid()!=null){
					Map<String, Object> glmap = new HashMap();
//					如果是关联差旅 则返回关联的差旅报销单id 以及差旅报销单摘要
					glID = bxzszpmxList.get(0).getClbxdid();
					glmap.put("glID",glID);
					glzy = bxService.getById(bxzszpmxList.get(0).getClbxdid()).getBxabstract();
					gllx = 1;
					glmap.put("glzy",glzy);
					glmap.put("gllx",gllx);
					glgx.add(glmap);
				}
			}
		}
		Bxsh bxsh = new Bxsh();
		bxsh.setBxdid(eaId);
		bxshList = bxshService.list(Condition.getQueryWrapper(bxsh));
		bxshVOList = BxshWrapper.build().listVO(bxshList);
		map.put("bx", bxVo);
		map.put("glgx", glgx);//关联关系
		map.put("bxxcmxList", bxxcmxVOList);
		map.put("bxfymxList", bxfymxVOList);
		map.put("bxzszpmxList", bxzszpmxVOList);
		map.put("bxshVOList", bxshVOList);
		return R.data(map);
	}

	/**
	 * 批量获取报销
	 *
	 * @param map id集合map
	 * @return
	 */
	@PostMapping("batchEA")
	public R<Map> getBatchExpenseAccount(@RequestBody Map<String, Object> map) {
		Bx bx;
		String idsStr = map.get("ids").toString();
		idsStr = idsStr.substring(1, idsStr.length() - 1).replace(" ","");
		List<Integer> ids = Func.toIntList(idsStr);
		List<Map<String, Object>> printData = new ArrayList<>();
		for (Integer id : ids) {
			bx = new Bx();
			bx.setId(id);
			R<Map> bxR = getExpenseAccountDetails(bx);
			if (bxR.isSuccess()) {
				printData.add(bxR.getData());
			}
		}
		//返回包装成前端视图数据
		return R.data(expenseAccountService.getPrintData(printData));
	}
	@GetMapping("/getUser")
	@ApiOperation(value = "人员筛选条件下拉框")
	public R<List<Map<String,Object>>> getUser(BladeUser user) {

		List<Map<String, Object>> list =null;
		Map<String, Object> params = new HashMap<>();
		params.put("id",user.getUserId());
		String roles = user.getRoleId();
		List<String> roleArr = null;
		if (Func.isNotBlank(roles)) {
			roleArr = Arrays.asList(roles.split(","));
		}
		try {
			if(roleArr.contains("1")||roleArr.contains("5")||roleArr.contains("4")){
				list=expenseAccountService.getUser(params);
			}else if (roleArr.contains("11")){
				params.put("roles","11");
				list=expenseAccountService.getUser(params);
			}else{
				list=new ArrayList<>();
				HashMap<String, Object> map = new HashMap<>();
				map.put(user.getUserId().toString(),user.getUserName());
				list.add(map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, list, "exception:" + e.getMessage());
		}
		return R.data(list,"查询成功");
	}
}
